const app = getApp()
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: 1,
    currentTab: 0,
    searchToday: 1,
    color: ["#FFE8DB", "#FFBFAE", "#D4EFDF", "#EFE4F3", "#C5EEFF", "#C5D5FF", "#D3C5FF", "#FFD9ED"],
    dayStyle: [],
    list: {
      // 格式
      // "2019-3": [{
      //     month: 'current',
      //     day: 10,
      //     background: '#FFE8DB'
      //   }]
    }
  },


  onLoad: function() {
    let identity = app.globalData.identityId
    let date = new Date(),
      realMonth = `${date.getFullYear()}-${date.getMonth() + 1}`,
      realMonthCalendarEnd = (date.getMonth() + 1) < 10 ? `${date.getFullYear()}-0${date.getMonth() + 1}` : `${date.getFullYear()}-${date.getMonth() + 1}`
    this.setData({
      nowYearTitle: date.getFullYear(),
      nowMonthTitle: date.getMonth() + 1,
      nowDayTitle: date.getDate(),
      realMonth: realMonth,
      realMonthCalendarStart: realMonthCalendarEnd,
      realMonthCalendarEnd: realMonthCalendarEnd,
      realDay: date.getDate(),
      common: app.globalData.features.indexOf("35") != -1 ? 0 : 1
    })
    if (this.data.common == 0) {
      this.getSignInListTeach()
      this.getTodayListTeac()
      this.getHistoryMeetingListTeac()
    } else {
      this.getSignInListStu()
      this.getTodayList()
      this.getHistoryListStu()
    }


  },


  dayClick: function(event) {
    console.log(event.detail)
    let clickDay = event.detail.day,
      changeDay = `dayStyle[${this.data.dayStyle.length - 1}].day`,
      selectMonth = `${event.detail.year}-${event.detail.month}`,
      selectDay = event.detail.day
    this.setData({
      [changeDay]: clickDay,
      nowYearTitle: event.detail.year,
      nowMonthTitle: event.detail.month,
      nowDayTitle: selectDay,
      selectMonth: selectMonth,
      selectDay: selectDay
    })
    if (this.data.common == 1 || this.data.currentTab == 1) {
      this.getTodayList()
    } else {
      this.getTodayListTeac()
    }


  },


  changeMonth: function(event) {
    console.log(event)
    let curMonth, falg
    if (event) {
      curMonth = `${event.detail.currentYear}-${event.detail.currentMonth}`
    } else {
      curMonth = this.data.realMonth
    }
    console.log(curMonth)
    let todayObj = {
      month: 'current',
      day: this.data.selectMonth ? (curMonth == this.data.selectMonth ? this.data.selectDay : null) : (curMonth == this.data.realMonth ? this.data.realDay : null),
      color: '#fff',
      background: '#FF6666'
    }
    console.log(this.data.list[curMonth])
    if (this.data.list[curMonth]) {
      let list = this.data.list[curMonth].map(item => item)
      list.push(todayObj)
      console.log(list)
      this.setData({
        dayStyle: list
      })
      return
    }
    this.setData({
      dayStyle: [todayObj]
    })
  },

  // 教师 日历接口
  getSignInListTeach() {
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getSignInListTeach',
      method: 'POST',
      data: {
        manager: app.globalData.userId
      },
      dataType: 'json',
      success: (res) => {
        let keys = []
        if (Object.keys(res.data).length != 0) {
          for (let key in res.data) {
            key = key.length == 7 ? key : `${key.slice(0, 5)}0${key.slice(5)}`
            keys.push(key)
          }
          keys.sort((a, b) => a.localeCompare(b))
          let realMonthCalendarStart = keys[0]
          this.setData({
            list: res.data,
            realMonthCalendarStart: realMonthCalendarStart
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络异常！',
          image: '/images/warning.png'
        })
      },
      complete: () => {
        this.changeMonth()
      }
    })
  },

  // 学生 日历接口
  getSignInListStu() {
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getSignInListStu',
      method: 'POST',
      data: {
        weChatId: app.globalData.wechatId
      },
      dataType: 'json',
      success: (res) => {
        let keys = []
        if (Object.keys(res.data).length != 0) {
          for (let key in res.data) {
            key = key.length == 7 ? key : `${key.slice(0, 5)}0${key.slice(5)}`
            keys.push(key)
          }
          keys.sort((a, b) => a.localeCompare(b))
          let realMonthCalendarStart = keys[0]
          this.setData({
            list: res.data,
            realMonthCalendarStart: realMonthCalendarStart
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络异常！',
          image: '/images/warning.png'
        })
      },
      complete: () => {
        this.changeMonth()
      }
    })
  },
  // 教师 指定日期签到情况
  getTodayListTeac() {
    this.setData({
      searchToday: 1 //wxml加载条
    })
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getSingInConditionTech',
      method: 'POST',
      data: {
        manager: app.globalData.userId,
        createDate: `${this.data.nowYearTitle}-${this.data.nowMonthTitle < 10 ? "0" + this.data.nowMonthTitle : this.data.nowMonthTitle}-${this.data.nowDayTitle < 10 ? "0" + this.data.nowDayTitle : this.data.nowDayTitle}`
      },
      dataType: 'json',
      success: (res) => {
        console.log(res)

        this.setData({
          nowMeetingListMa: res.data
        })
      },
      complete: () => {
        this.setData({
          searchToday: 0
        })
      }
    })
  },

  // 学生 指定日期签到情况
  getTodayList() {
    this.setData({
      searchToday: 1 //wxml加载条
    })
    //获取当天会议情况
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getUserCheckInfoStu',
      method: 'POST',
      data: {
        weChatId: app.globalData.wechatId,
        createDate: `${this.data.nowYearTitle}-${this.data.nowMonthTitle < 10 ? "0" + this.data.nowMonthTitle : this.data.nowMonthTitle}-${this.data.nowDayTitle < 10 ? "0" + this.data.nowDayTitle : this.data.nowDayTitle}`
      },
      dataType: 'json',
      success: (res) => {
        console.log(res)

        if (res.data) {
          this.setData({
            nowMeetingList: res.data
            // nowMeetingList: [{
            //   name: `计算机技术${this.data.nowDayTitle}`,
            //   late: 0,
            //   signInCount: 20
            // }
            // ]
          })
        } else {
          this.setData({
            nowMeetingList: []
          })
        }
      },
      complete: () => {
        this.setData({
          searchToday: 0
        })
      }
    })
  },

  //教师 所有签到记录
  getHistoryMeetingListTeac() {
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getMeetingInfo',
      method: 'POST',
      data: {
        manager: app.globalData.userId
      },
      dataType: 'json',
      success: (res) => {
        console.log(res)
        this.setData({
          historyMeetingInfo: res.data
        })
      },
      complete: () => {
        this.setData({
          loading: 0
        })
      }
    })
  },
  //教师查看一场会议历史签到情况
  stuOneMeeting(e) {
    console.log(e)
    wx.navigateTo({
      url: `/pages/signInStatisticsOneMeeting/signInStatisticsOneMeeting?id=${e.currentTarget.dataset.id}&name=${e.currentTarget.dataset.name}&type=${e.currentTarget.dataset.type}`,
    })


  },

  //学生 所有签到记录
  getHistoryListStu() {
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getAllCheckInfoByWeChat',
      method: 'POST',
      data: {
        weChatId: app.globalData.wechatId
      },
      dataType: 'json',
      success: (res) => {
        console.log(res)
        this.setData({
          historyMeetingList: res.data
        })
      },
      complete: () => {
        this.setData({
          loading: 0
        })
      }
    })
  },

  toSignInIndex() {
    if (app.globalData.features.indexOf("35") != -1){
      wx.redirectTo({
        url: '/pages/signInIndex/signInIndex?fromSta=1',
      })
    }else{
      wx.redirectTo({
        url: '/pages/signInIndex/signInIndex',
      })
      // util.scanQRCode()
    }
  },

  bindChange: util.throttle(function(e) {
    console.log(e)
    this.setData({
      loading: 1,
      currentTab: e.detail.current,
      selectMonth: null,
      selectDay: null
    })
    if (this.data.currentTab == 0) {
      this.getSignInListTeach()
      this.getTodayListTeac()
      this.getHistoryMeetingListTeac()
    } else {
      this.getSignInListStu()
      this.getTodayList()
      this.getHistoryListStu()
    }
  }, 300),

  swichNav: function(e) {
    this.setData({
      loading: 1,
      currentTab: e.currentTarget.dataset.current,
      selectMonth: null,
      selectDay: null
    })
    if (this.data.currentTab == 0) {
      this.getSignInListTeach()
      this.getTodayListTeac()
      this.getHistoryMeetingListTeac()
    } else {
      this.getSignInListStu()
      this.getTodayList()
      this.getHistoryListStu()
    }
  },
})